

export class DFWebSocket {
  socket: any = '';
  setIntervalWesocketPush: any = null;
  connectUrl: string = '';
  errorNum: number = 1;
  timeOut = 3000;
  timeOutFn: any = null;
  timeOutClose: any = null;
  callBackFn = null;

  constructor(options) {
    const { url, callBack } = options;

    this.createSocket(url, callBack,true);
  }

  createSocket: any = (url, callBack, firstCreate = false) => {
    clearTimeout(this.timeOutFn);
    clearTimeout(this.timeOutClose);
    this.socket && this.socket.close();
    if (!this.socket) {
      this.connectUrl = url;
      console.log('建立websocket连接');
      this.socket = new WebSocket(this.connectUrl || url);
      this.socket.onopen = () => {
        this.onopenWS();
      };
      this.socket.onmessage = e => {
        if (callBack) {
          firstCreate && (this.callBackFn = callBack);

          callBack(e);
        }
      };
      this.socket.onerror = () => {
        this.onerrorWS();
      };
      this.socket.onclose = () => {
        console.log(222)
        this.oncloseWS();
      };
    } else {
      console.log('websocket已连接');
    }
  };

  onopenWS = () => {
    this.errorNum = 0;
    this.sendPing();
  };

  onerrorWS = () => {
    if (this.errorNum > 30) return;
    this.errorNum = this.errorNum + 1;
    this.socket?.close();
    clearInterval(this.setIntervalWesocketPush);
    console.log('连接失败重连中');
    this.timeOut = this.timeOut + 3000;
    this.socket = null;
    this.timeOutFn = setTimeout(() => {
      this.createSocket(this.connectUrl, this.callBackFn);
    }, this.timeOut);
  };

  closeWS = () => {
    this.socket && this.socket.close();
  };

  connecting = message => {
    if (this.errorNum > 30) return;

    setTimeout(() => {
      console.log(this.socket);
      if (!this.socket || this.socket.readyState === 0) {
        this.onerrorWS();

        // this.connecting(message);
      } else {
        this.socket.send(JSON.stringify(message));
      }
    }, 1000);
  };

  sendWSPush = message => {
    if (!this.socket) {
      return;
    };

    // console.log(this.socket.readyState);

    if (this.socket.readyState === 3) {
      this.socket.close();
      this.createSocket(this.connectUrl);
    } else if (this.socket.readyState === 1) {
      this.socket.send(JSON.stringify(message));
    } else if (this.socket.readyState === 0) {
      this.connecting(message);
    }
  };

  oncloseWS = () => {
    // if(errorNum>30)return
    // errorNum = errorNum+1
    clearInterval(this.setIntervalWesocketPush);
    console.log('websocket已断开....正在尝试重连');
    if (!this.socket || this.socket.readyState !== 2) {
      this.socket = null;
      this.timeOut = this.timeOut + 3000;
      this.timeOutClose = setTimeout(() => {
        this.createSocket(this.connectUrl);
      }, this.timeOut);
      // createSocket(connectUrl)
    }
  };

  sendPing = (time = 5000, ping = 'ping') => {
    clearInterval(this.setIntervalWesocketPush);
    this.socket.send(ping);
    this.setIntervalWesocketPush = setInterval(() => {
      this.socket.send(ping);
    }, time);
  };
}
